package com.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.entity.Article;
import com.entity.Comment;
import com.entity.User;
import com.service.ArticleService;

import com.service.UserService;

@Controller @RequestMapping("/admin")
public class AdminController {
	
	@Resource
	private  UserService userService;
	
	@Resource
	private ArticleService articleService;
	

	
	//登录5条最近文章和评论
	@RequestMapping("/login")
	public String login(String userName,String userPass,ModelMap m,HttpSession session) {
		User user=userService.login(userName, userPass);
		if(user!=null) {
			if(user.getUserStatus()!=0) {
				session.setAttribute("session_user", user);
				
				//最近的五条文章
				List<Article> articleList = articleService.listRecentArticle(5);
				m.put("articleList", articleList);
				
				return "index";
			}else {
				m.put("msg", "用户已被锁，请联系管理员");
				return "login";
			}
		}
		else {
			m.put("msg","该账号不存在或密码错误");
			return "login";
		}
	}
	
	
	//点击添加用户超链接进入到添加用户界面
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public String gotoAddPage() {
		return "User/user-add";
	}
	
	//点击添加用户界面保存进入此方法
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(User user,MultipartFile photo) throws IOException {
		user.setUserRegisterTime(new Date());
		user.setUserStatus(1);
		user.setUserPhoto(photo.getBytes());//getBytes:把文件变成字节数
		userService.addUser(user);
		
		return "redirect:/admin/user"; //添加完成以后，转到用户列表页
	}

	//点击全部用户进入此方法返回到用户列表页
	@RequestMapping("user")
	public String users(ModelMap m) {
		List<User> userList = userService.listUser();
		m.put("userList", userList);
		return "User/user-list";

	}
	
	//根据照片src地址找到此方法显示用户的照片
	@RequestMapping("/photo/{id}")
	//@PathVariable:可以将url中的占位符id绑定在控制器处理方法的入参中
	public void showPhoto(@PathVariable("id") Integer id,HttpServletResponse response) throws IOException {
		User user= userService.getUserById(id);
		response.setContentType("image/jpg"); //设置给浏览器的数据类型为照片jpg
		ServletOutputStream outStream = response.getOutputStream(); //从response得到一个输出流
		outStream.write(user.getUserPhoto()); //输出user表中的图片数据
		outStream.flush(); //刷缓冲(防止输出流写到缓冲区里看不到图片)
	}
	
	//点击用户列表的编辑进入此方法
	@RequestMapping(value="/update/{id}",method=RequestMethod.GET)
	public String gotoupdatePage(@PathVariable("id") Integer id,ModelMap m) throws IOException {
		User user = userService.getUserById(id);
		m.put("user",user);
		return "User/user-update";
	}
	
	//点击用户编辑的保存进入此方法
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(MultipartFile photo,HttpServletRequest request) throws IOException {
		User user = new User();
		user.setUserId(Integer.parseInt(request.getParameter("userId"))); //提取value(从提交的表单中标签input的name为userId)
		user.setUserName(request.getParameter("userName"));
		user.setUserPass(request.getParameter("userPass"));
		user.setUserNickname(request.getParameter("userNickname"));
		user.setUserEmail(request.getParameter("userEmail"));
		user.setUserUrl(request.getParameter("userUrl"));
		user.setUserStatus(Integer.parseInt(request.getParameter("userStatus")));
		user.setUserPhoto(photo.getBytes());//getBytes:把文件变成字节数
		userService.deleteUser(Integer.parseInt(request.getParameter("userId")));
		userService.addUser(user);
		
		return "redirect:/admin/user"; //更新后去到列表页查询用户
	}
	
	//点击用户删除进入此方法
	@RequestMapping("/delete/{id}")
	public void deleteUser(@PathVariable("id") Integer id) {
		userService.deleteUser(id);
	}
	
	
	
	
	
	
	
	
	
	
}
